﻿//DevNETCoder
//CopyRight By DevNET Group
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using CuoiHoi.Entities;
using CuoiHoi.DataAccess;

namespace CuoiHoi.Business
{
    public class BHinhAnh
    {
//-----------------------------------------------------------//
        public static DataTable SelectAll()
        {
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "HinhAnh_SelectAll", null);
            return all;
        }
        public static DataTable SelectTop(int Top)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Top", Top);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "HinhAnh_SelectTop", pr);
            return all;
        }
//-----------------------------------------------------------//
        public static void Insert(EHinhAnh OHinhAnh)
        {
            SqlParameter[] pr = new SqlParameter[3];
            pr[0] = new SqlParameter(@"TenHinh", OHinhAnh.TenHinh);
            pr[1] = new SqlParameter(@"DuongDanHinh", OHinhAnh.DuongDanHinh);
            pr[2] = new SqlParameter(@"idAlbum", OHinhAnh.idAlbum);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"HinhAnh_Insert", pr);
        }
//-----------------------------------------------------------//
        public static void Update(EHinhAnh OHinhAnh)
        {
            SqlParameter[] pr = new SqlParameter[4];
            pr[0] = new SqlParameter(@"idHinhAnh", OHinhAnh.idHinhAnh);
            pr[1] = new SqlParameter(@"TenHinh", OHinhAnh.TenHinh);
            pr[2] = new SqlParameter(@"DuongDanHinh", OHinhAnh.DuongDanHinh);
            pr[3] = new SqlParameter(@"idAlbum", OHinhAnh.idAlbum);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"HinhAnh_Update", pr);
        }
//-----------------------------------------------------------//
        public static void Delete(int idHinhAnh)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"idHinhAnh",idHinhAnh);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "HinhAnh_Delete", pr);
        }
//-----------------------------------------------------------//
        public static EHinhAnh SelectByID(int idHinhAnh)
        {
            EHinhAnh OHinhAnh = new EHinhAnh();
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"idHinhAnh",idHinhAnh);
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure,"HinhAnh_SelectByID", pr);
            if (idr.Read())
              OHinhAnh = GetOneHinhAnh(idr);
            idr.Close();
            idr.Dispose();
            return OHinhAnh;
        }
        public static EHinhAnh SelectByIdAlbum(int idAlbum)
        {
            EHinhAnh OHinhAnh = new EHinhAnh();
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"idAlbum", idAlbum);
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "HinhAnh_SelectByIdAlbum", pr);
            if (idr.Read())
                OHinhAnh = GetOneHinhAnh(idr);
            idr.Close();
            idr.Dispose();
            return OHinhAnh;
        }
//-----------------------------------------------------------//
        private static EHinhAnh GetOneHinhAnh(IDataReader idr)
       {
            EHinhAnh OHinhAnh = new EHinhAnh();
                if (idr["idHinhAnh"] != DBNull.Value)
                    OHinhAnh.idHinhAnh = (int)idr["idHinhAnh"];
                if (idr["TenHinh"] != DBNull.Value)
                    OHinhAnh.TenHinh = (string)idr["TenHinh"];
                if (idr["DuongDanHinh"] != DBNull.Value)
                    OHinhAnh.DuongDanHinh = (string)idr["DuongDanHinh"];
                if (idr["idAlbum"] != DBNull.Value)
                    OHinhAnh.idAlbum = (int)idr["idAlbum"];
            return OHinhAnh;
       }
//-----------------------------------------------------------//
        public static bool TestByID(int idHinhAnh)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"idHinhAnh",idHinhAnh);
            pr[1] = new SqlParameter(@"TestID",SqlDbType.Bit);
            pr[1].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"HinhAnh_TestByID", pr);
            return Convert.ToBoolean(pr[1].Value);
        }
//-----------------------------------------------------------//
        public static List<EHinhAnh> ListAll()
        {
            List<EHinhAnh> list = new List<EHinhAnh>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "HinhAnh_SelectAll", null);
            while (idr.Read())
              list.Add(GetOneHinhAnh(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }

        public static List<EHinhAnh> ListTop(int Top)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Top", Top);
            List<EHinhAnh> list = new List<EHinhAnh>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "HinhAnh_SelectAll", pr);
            while (idr.Read())
              list.Add(GetOneHinhAnh(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }

        // viết thêm 
        public static DataTable SelectHinhAnhByAlbum(int idAlbum)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"idAlbum", idAlbum);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "HinhAnh_SelectHinhAnhByAlbum", pr);
            return all;
        }

    }
 
}